package com.freshcity.page.feiji
{
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.Window;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.feiji.FeiJiWriteWin;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.utils.MessageUtil;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenMax;
	
	import fl.controls.UIScrollBar;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;

	public class FeiJiWriter extends Component
	{
		private var back:FeiJiWriteWin;
		private var vScrollBar:UIScrollBar;
		private var defaultMsg:String;

		public function FeiJiWriter(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0,defaultMsg:String=null)
		{
			this.mouseEnabled=false;
			this.tabEnabled=false;
			this.visible=false;
			this.defaultMsg = defaultMsg;
			super(parent, xpos, ypos);
			
			back.confirmBtn.addEventListener(MouseEvent.CLICK, onSubmit);
			back.words.addEventListener(Event.CHANGE, onPicContentChange);
			back.words.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
		}
		
		private function focusInHandler(e:FocusEvent):void
		{
			//IME.enabled = true;
			back.words.text = "";
		}
		
		public function onShow(parent:DisplayObjectContainer=null):void
		{
			back.confirmBtn.enabled=false;
			if(this.defaultMsg==null){
				back.words.htmlText = MessageUtil.getFeijiGuildWords();
			}else{
				back.words.htmlText = this.defaultMsg;
			}
			
			if(this.parent!=null) this.parent.removeChild(this);
			if(parent){
				parent.addChild(this);
			}else{
				var win:Window = new Window(null,0,0,"纸飞机");
				win.hasCloseButton=true;
				win.addChild(this);
				win.layer = LayerManager.popupLayer;
				win.layer.addPopUp(win);
				win.setSize(400,340);
				//EffectManager.addUIMask([],0);
			}
			
			TweenMax.to(this,0.3,{autoAlpha:1});
			//this.stage.focus = back.words;
			//var l:int = back.words.length;
			//back.words.setSelection(l,l);
			//EffectManager.addUIMask([],0);			
		}

		private function onSubmit(event:Event):void
		{
			var len:int = StringUtil.getANSILength(back.words.text);
			if(len<8){
				main.msg.showRollTipsMsg("内容太简单啦");
				return;
			}
			sendServer();
		}
		
		private function sendServer():void
		{
			var c:String = StringUtil.trim(back.words.text);
			main.commu.send(OpCodes.WRITE_FEIJI, {content:c}, onFeedBack);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new FeiJiWriteWin();
			addChild(back);
			
			vScrollBar = new UIScrollBar();
			vScrollBar.scrollTarget = back.words;
			vScrollBar.height = back.words.height;
			vScrollBar.move(back.words.x + back.words.width, back.words.y);
			back.addChild(vScrollBar);
		}
		
		override public function destroy():void
		{
			back.confirmBtn.removeEventListener(MouseEvent.CLICK, onSubmit);
			back.words.removeEventListener(Event.CHANGE, onPicContentChange);
			
			if (parent)parent.removeChild(this);
		}


		protected function onPicContentChange(event:Event):void
		{
			var c:String=StringUtil.trim(back.words.text);
			if (c.length <= 0)
			{
				back.confirmBtn.enabled=false;
			}
			else
			{
				back.confirmBtn.enabled=true;
				this.vScrollBar.update();
			}
		}
		
		override public function hide(event:Event=null):void
		{
			if(this.parent.parent is Window){
				Window(this.parent.parent).hide();
			}else{
				main.commu.send(OpCodes.READ_MY_FEIJI, {});
			}
			super.hide(event);
		}

		private function onFeedBack(data:*):void
		{
			if (data == true)
			{
				main.msg.showRollTipsMsg("纸飞机已投出");
				this.hide();
			}
			else if(data == false)
			{
				main.msg.showRollTipsMsg("今日可投纸飞机名额已用完!再投需等明日。");
			}
		}
	}
}